<?php
/**
 * Created by PhpStorm.
 * User: yangzhic
 * Date: 2017/4/12
 * Time: 16:15
 */

namespace SDXApi\Service;
use Think\Cache\Driver\Redis;
use Think\Exception;
class QueueService
{
    protected $queue;

    public function __construct()
    {
        $this->queue = new Redis();
    }


    /**
     * 加入队列末尾进行排队
     * @param $key
     * @param $queue_code
     */
    public function add_queue($key,$queue_code){
        if(empty($this->queue)){
            $this->queue = new Redis();
        }
        //清除已存在队列的元素，重新排队
        $this->remove_queue($key,$queue_code);

        $time_out = gmtime()+30;
        $data = array('code'=>$queue_code,'time_out'=>$time_out);
        $this->queue->rPush($key,$queue_code);
        S($key.'_'.$queue_code,$data,30);
    }

    /**
     * 从队列中去掉指定的元素
     * @param $key
     * @param $queue_code
     */
    public function remove_queue($key,$queue_code){
        if(empty($this->queue)){
            $this->queue = new Redis();
        }
        $this->queue->lRem($key,$queue_code,0);
        S($key.'_'.$queue_code,null);
    }

    /**
     * 从队列中去掉第一个元素
     * @param $key
     */
    public function lpop_queue($key){
        if(empty($this->queue)){
            $this->queue = new Redis();
        }
        $this->queue->lPop($key);
    }

    /**
     * @param int $length
     * @return int|string
     */
    public function get_order_queue_code($length=6){
        $code = time();

        $ranstr = getRndstr($length);
        $code .= $ranstr;
        return $code;
    }

    /**
     * 检查订单队列
     * @param $key
     * @param $queue_code
     * @param $goods_list
     * @param $param
     * @return array
     */
    public function check_order_queue($key,$queue_code,$goods_list,$param){
        $flag = true;
        $res = array('error'=>0,'msg'=>'success');
        try{
            if(empty($this->queue)){
                $this->queue = new Redis();
            }
            while($flag){
                $code = S($key.'_'.$queue_code);
                if(!empty($code)){
                    $current_order_list = $this->queue->lRange($key,0,0);
                    if($queue_code==$current_order_list[0]){
                        $flag=false;
                        //检查商品库存
                        $dtime=date('Y-m-d',time());
                        $dbesttime=date('Y-m-d',$param['best_time']);
                        $is_morrow=0;//是否次日送达，若是，则1
                        if($dtime!=$dbesttime){
                            $is_morrow=1;
                        }
                        $result = D('goods')->check_queue_order_goods_stock($goods_list,$param['wid'],$is_morrow,$is_store);
                        $this->remove_queue($key,$queue_code);
                        if($result){
                            $res = $result;
                        }
                    }else{
                        usleep(200000); //延迟0.2秒后执行
                    }
                }else{
                    $flag=false;
                    $this->remove_queue($key,$queue_code);
                    $res = array('error'=>1,'msg'=>'订单提交超时，请重新提交订单');
                }
            }
        }catch (Exception $e){
            $this->remove_queue($key,$queue_code);
            $res = array('error'=>1,'msg'=>'订单提交出错，请重新提交订单');
        }

        return $res;
    }
}